var tabs = {
	sysName: "",
	init:function(){
		this.initPara();
		this.html5();
		this.bind();
		
	},
	initPara:function(){
		this.sysName = tools.getQueryString("sysName")==""?"sys_name_1":tools.getQueryString("sysName");
		
	},	
	html5:function(){
		$("head  title").text(config.name);
		$(".w-logo").text(config.name);
		$(".footer").text(config.footer);
		//this.topMenu();
		this.initMenu();		
		this.initTabs();
	},
	
	topMenu: function(){
		var that = this;
		tools.ajaxRequestAsync("../menu/sysMenus.mvc", {}, function(data){
			console.log(data);
			if(data.respCode=="0"){
				if(data.list.length==0){
					that.showError("顶部菜单信息为空");
					return false;
				}else{
					$.each(data.list,function(i,n){
						if (n.exist=="1"){
							//点击设置sysName; 刷新左部菜单
							$("#topMenu").append("<li><a data-sys='"+n.code+"'>"+n.code_desc+"</a></li>");
						}
					});					
				}				
			}else{
				that.showError("顶部菜单信息获取失败");
				return false;
			}			
		});		
		
	},
	
	initMenu:function(){
		$("#menu").empty();
		//$("#menu").append("<li><a data-title='首页' data-close='false' data-addtab='main' data-url='sy.html'><span class='list-group-item' id='main'>首页</span></a></li>");
		var reqData={    
	    		"sysName" : this.sysName
	    };
		var data= tools.ajaxRequest("../pub/menu/sysMenu",reqData);	
		if (data.respCode != -1) {
			$("#tabs1").empty();
			$("#home").remove();
			$("#menu").append(data.ret);
			$('#menu ul').hide();
			$('#menu > li a').each(function(){
				var ul = $(this).next();
				if(!ul[0] || !ul.children().length){
					$(this).find("em").hide();
				}
			});	
			$('#menu > li a').each(function() {
				$(this).attr("title", $(this).text());
			})
			
		}else{
			this.showError("获取菜单信息失败");
			$("#home").text("获取菜单信息失败");
			return false;
		}
		
	},
	
	initTabs:function(){
		var that = this;
		$.addtabs({"target":"#tabs1","iframe": true, "contextmenu": false,"unclose":["main"],
			callback: function() {
				that.checkTab();
			}
		});
//		$('#menu li a:contains(排行榜)').trigger('click');//wxq add
		$('#menu li a:contains(排行榜)').parent().parent().show();//wxq add
		$("#main").parent().trigger("click");
		//$("#menu").trigger("click");
		this.checkTab();
	},
	
	checkTab: function() {
		var items = $("#tabs1").children('li[role="presentation"]');
		console.log(items.length)
		if(items.length > 1){
			$("#tabs1").removeAttr("style");
			
		}
		else {
			$("#tabs1").css({height: 0, opacity: 0, overflow: "hidden", "margin-bottom": "0"});
		}
	},
	
	bind:function(){
		var that = this;
		
		$("#userhelp").on("click",	function() {

					var path = "/pub/help/doc/user.docx";
					var name = "天津联通商城后台使用手册.docx";
					var form = "<form id='download' action='../attach/download.mvc'>";
					form += "<input type='hidden' name='filename'  value='"+name+"' />";
					form += "<input type='hidden' name='filepath'  value='"+path+"' />";
					form += "</form>";
					$(".wrap").append(form);
					$("#download").submit();
					$("#download").remove();

		});
		
		$("#menu").on("click","li span",function(){
			if(!$(this).find("em").is(":visible")){
				return;
			}
			var checkElement = $(this).next();
			if ((checkElement.is('ul')) && (checkElement.is(':visible'))) {
				checkElement.slideUp('normal');
				$(this).find("em").removeClass('glyphicon-chevron-up');
				$(this).find("em").addClass('glyphicon-chevron-down');
				return false;
			}
			if ((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
				//$('#menu ul:visible').slideDown('normal');
				$(this).find("em").removeClass('glyphicon-chevron-down');
				$(this).find("em").addClass('glyphicon-chevron-up');
				checkElement.slideDown('normal');
				return false;
			}
		});
		setTimeout(function(){
			if (!config.menuNameSearch) {
				return;
			}
			var arr = $('#menu > li').find("a");
			for (var i = 0; i < arr.length; i++) {
				if ($(arr[i]).text() == config.menuNameSearch) {
					$(arr[i]).click();
					return;
				}
			}
			$('#menu > li').eq(config.menuDefault).find("span").click();
		},0);
		
		//点击左边nav增加active样式
		$("#menu").on("click","li",function(e){
		  if($(this).find("li").length){
		    return;
		  }
		  //$("#menu li").removeClass("active");
		  $(this).addClass("active");
		  $("#menu li").not(this).removeClass("active");
		  // e.stopPropagation();		  
		});
		//tab页面切换后,左边的nav激活acitve
		$("#tabs1").on("click", 'li[role="presentation"] a', function(){
		    var n = $(this).text();
		    var navItem = $("#menu").find('a[data-title="' + n + '"]');
		    if(!navItem.length){
		      return;
		    }
		    navItem.parent().addClass("active");
		    if(navItem.parent()[0].scrollIntoView){
		    	navItem.parent()[0].scrollIntoView();
		    }
		    $("#menu li").not(navItem.parent()).removeClass("active");
		    var box = navItem.parent().parent();
		    //没展开的展开一下
		    if(box.is("ul") && !box.is(":visible")){
		      box.prev().trigger("click");
		    }
		})
			
		$(".bar-con-l").on("click",function(){
			if($(".left_side").is(':hidden')){
				$(".left_side").show();
			}else{
				$(".left_side").hide();
			}			
		});
		
		$("#topMenu").on("click","a",function(){
			that.sysName = $(this).data("sys");
			$.addtabs.closeAll('#tabs1');
			that.initMenu();
			that.initTabs();
		});
		
	},
	
	showError:function(){
		var msg = "加载数据错误";
		if(arguments[0]!=undefined){
			msg = arguments[0];
		}
		alert(msg);
	}
	
};

$(function(){
	tabs.init();
	window.name="主页";
});
//20190307测试结果 必须鼠标点一下页面, return的字符串无法显示出来,文字是固定的. return null 和 undefined 无效, false和空可以
window.onbeforeunload = function(e) {
    return e = e || window.event,
    e && (e.returnValue = "关闭浏览器聊天内容将会丢失。"),
    "关闭浏览器聊天内容将会丢失。"
}

